// 餐品模型
import { DataTypes } from 'sequelize'
import { sequelize } from '../../db/index.js'
import FoodCategory from '../FoodCategory/FoodCategoryModel.js'

const Food = sequelize.define('Food', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: '餐品ID'
  },
  name: {
    type: DataTypes.STRING(100),
    allowNull: false,
    comment: '餐品名称'
  },
  price: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    comment: '餐品价格'
  },
  description: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '餐品描述'
  },
  image: {
    type: DataTypes.STRING,
    allowNull: true,
    defaultValue: '/uploads/foods/default-food.png',
    comment: '餐品图片'
  },
  category_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '所属分类ID',
    references: {
      model: FoodCategory,
      key: 'id'
    }
  },
  status: {
    type: DataTypes.ENUM('available', 'unavailable'),
    defaultValue: 'available',
    comment: '状态：available - 可售，unavailable - 停售'
  },
  stock: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: '库存数量'
  },
  sales: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: '销售量'
  }
}, {
  tableName: 'food',
  timestamps: true,
  paranoid: true, // 软删除
  underscored: true,
  indexes: [
    {
      name: 'idx_category',
      fields: ['category_id']
    }
  ]
});

// 建立与分类的关联关系
Food.belongsTo(FoodCategory, {
  foreignKey: 'category_id',
  as: 'category'
});

FoodCategory.hasMany(Food, {
  foreignKey: 'category_id',
  as: 'foods'
});

export default Food;